package android.tkb.activity.word;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.tkb.dao.word.DatabaseHelper;
import android.tkb.model.word.Words;
import android.tkb.util.word.Contants;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * 显示信息页面
 * @author Administrator
 *
 */
public class ShowWordMessageActivity extends Activity {
	
	private int listCount =0;
	private int index =0;
	
	private String value = Contants.E ;//给定一个初使值
	
	private TextView showTv1,showTv2,showTv3;
	private ImageView showTvFlag;
	private Button butUp,butNext,back;
	private Cursor cur;
	
	private List<Words> result;
	private Words words ;
	public void onCreate(Bundle savedInstanceState) {
		   
        super.onCreate(savedInstanceState);
        setTitle(R.string.word_show);
        setContentView(R.layout.show_word_message);
        
        /* 从上一下物件接收内容++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
        Intent intent = getIntent();
		words = (Words) intent.getSerializableExtra("words");
		index = intent.getIntExtra("index", 0);
		value = intent.getStringExtra("flag");//各视图页面标识
		
		
		 back = (Button) findViewById(R.id.back);
		 if(Contants.E.equals(value) ||Contants.C.equals(value)){
			 back.setText("测试结果");
		 }else if(Contants.ER.equals(value)){
			 back.setText("易错单词");
		 }else if(Contants.S.equals(value)){
			 back.setText("单词搜索");
		 }
			
		 back.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				System.out.println(value+"========================================");
				Intent intent = new Intent();
				if(Contants.E.equals(value) || Contants.C.equals(value)){
					intent.setClass(ShowWordMessageActivity.this, TestResultActivity.class);
					
					intent.putExtra("words", words);
					intent.putExtra("flag", value);
					
				}else if(Contants.ER.equals(value)){
					intent.setClass(ShowWordMessageActivity.this, EasyWordActivity.class);
				}else if(Contants.S.equals(value)){
					intent.setClass(ShowWordMessageActivity.this, SearchWordActivity.class);
					
				}
				startActivity(intent);
				finish();
			}
			 
		 });

		result = words.getPrefeList();
		
		listCount = result.size();
		
		handleDate(result,index,value);
		
		butUp = (Button) findViewById(R.id.showBut1);
		butNext = (Button) findViewById(R.id.showBut2);
		
		if(listCount == 0){
			butUp.setClickable(false);
			butNext.setClickable(false);
		}else{
			butUp.setOnClickListener(new ButUp());
			butNext.setOnClickListener(new ButNext());
		}
		
		if(index == listCount-1){
			butNext.setClickable(false);
		}
		
		if(index == 0){
			butUp.setClickable(false);
		}
		
		
	}
	//上一下单词
	private class ButUp implements OnClickListener{

		@Override
		public void onClick(View v) {
			index--;
			if(index < 1){
				index = 0;
				butUp.setClickable(false);
			}else{//可见
				
				
				butUp.setClickable(true);
				butNext.setClickable(true);
			}
			handleDate(result,index,value);
			
		}

	}
	//下一下单词
	private class ButNext implements OnClickListener{

		@Override
		public void onClick(View v) {
			index++;
			if(index >= listCount-1){
				index = listCount-1;
				butNext.setClickable(false);
			}else{//可见
				
				
				butUp.setClickable(true);
				butNext.setClickable(true);
			}
			handleDate(result,index,value);
			
		}

	}
	
	public void handleDate(List<Words> result1,int index1,String flag){
		if(result1.size()>0){//表明有数据(正确转向)
			Words w = result1.get(index1);
			showTv1 = (TextView) findViewById(R.id.showTv1);
			showTv2 = (TextView) findViewById(R.id.showTv2);
			showTvFlag = (ImageView) findViewById(R.id.showTvFlag);
			
			String state = w.getTempState();//处理显示应状态不同的图
			if(Contants.S.equals(state)){
				showTvFlag.setBackgroundResource(R.drawable.s);
			}else if(Contants.ER.equals(state)){
				showTvFlag.setBackgroundResource(R.drawable.er);
			}else if(Contants.V.equals(state)){
				showTvFlag.setBackgroundResource(R.drawable.ok);
			}else if(Contants.X.equals(state)){
				showTvFlag.setBackgroundResource(R.drawable.off);
			}
			
			if(Contants.E.equals(flag) || Contants.S.equals(flag) || Contants.ER.equals(flag)){//中英处理
				showTv1.setText(w.getEn());
				showTv2.setText(w.getJp());
			}else if(Contants.C.equals(flag)){
				showTv2.setText(w.getEn());
				showTv1.setText(w.getJp());
			}
			
			showTv3 = (TextView) findViewById(R.id.showTv3);
			DatabaseHelper mOpenHelper = new DatabaseHelper(ShowWordMessageActivity.this);
			SQLiteDatabase db = mOpenHelper.getReadableDatabase();
			
			cur = db.query("words", new String[]{"right_times","wrong_times"}, "en = ?", new String[]{w.getEn()}, null, null, null);			
			while (cur.moveToNext()) {
				int rightTimes = cur.getInt(cur.getColumnIndex("right_times"));
				int wrongTimes = cur.getInt(cur.getColumnIndex("wrong_times"));
				
				showTv3.setText("正确次数:"+rightTimes +"    错误次数："+wrongTimes);
			}
			
			cur.close();
		    db.close();
		    mOpenHelper.close();
		    
		}
	}
	
	
}
